package cn.stylefeng.guns.modular.robot.service.impl;

import cn.stylefeng.guns.core.common.exception.BizExceptionEnum;
import cn.stylefeng.guns.core.common.page.LayuiPageFactory;
import cn.stylefeng.guns.core.common.page.LayuiPageInfo;
import cn.stylefeng.guns.core.shiro.ShiroKit;
import cn.stylefeng.guns.core.shiro.ShiroUser;
import cn.stylefeng.guns.modular.robot.entity.CdInfo;
import cn.stylefeng.guns.modular.robot.entity.DfInfo;
import cn.stylefeng.guns.modular.robot.entity.WalletConfig;
import cn.stylefeng.guns.modular.robot.mapper.WalletConfigMapper;
import cn.stylefeng.guns.modular.robot.service.CdInfoService;
import cn.stylefeng.guns.modular.robot.service.DfInfoService;
import cn.stylefeng.guns.modular.robot.service.WalletConfigService;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.roses.kernel.model.exception.ServiceException;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

@Service
public class WalletConfigServiceImpl extends ServiceImpl<WalletConfigMapper, WalletConfig> implements WalletConfigService {

    @Resource
    private CdInfoService cdInfoService;
    @Resource
    private DfInfoService dfInfoService;

    @Override
    public WalletConfig getWalletConfig(Integer type, String imeis) {
        WalletConfig walletConfig = new WalletConfig();
        List<WalletConfig> walletConfigs = lambdaQuery()
                .eq(WalletConfig::getType, type)
                .in(WalletConfig::getImei, Arrays.asList(imeis.split(",")))
                .eq(WalletConfig::getEnable, true)
                .list();
        if (CollectionUtils.isEmpty(walletConfigs)) {
            return walletConfig;
        }
        walletConfig = walletConfigs.get(0);
        walletConfig.setCreateTime(null);
        return walletConfig;
    }

    @Override
    public void change(WalletConfig walletConfig) {
        List<WalletConfig> walletConfigs = lambdaQuery()
                .eq(WalletConfig::getCreateBy, walletConfig)
                .eq(WalletConfig::getType, walletConfig.getType())
                .eq(WalletConfig::getIsMax, false)
                .list();
        if (CollectionUtils.isEmpty(walletConfigs)) {
            return;
        }
        WalletConfig wc = walletConfigs.get(0);
        wc.setEnable(true);
        updateById(wc);
    }

    @Override
    public void add(WalletConfig walletConfig) {
        String imei = walletConfig.getImei();
        List<WalletConfig> list = lambdaQuery()
                .eq(WalletConfig::getImei, imei)
                .list();
        if(!CollectionUtils.isEmpty(list)){
            throw new ServiceException(BizExceptionEnum.IMEI_EXISTS);
        }
        ShiroUser shiroUser = ShiroKit.getUserNotNull();
        walletConfig.setCreateBy(shiroUser.getAccount());
        walletConfig.setCreateTime(new Date());
        save(walletConfig);
    }

    @Override
    public void update(WalletConfig walletConfig) {
        updateById(walletConfig);
    }

    @Override
    public ResponseData detail(Long id) {
        ShiroUser user = ShiroKit.getUserNotNull();
        WalletConfig walletConfig = getById(id);
        if(!user.getAccount().equals(walletConfig.getCreateBy())){
            return ResponseData.success();
        }
        return ResponseData.success(walletConfig);
    }

    @Override
    public LayuiPageInfo list(String imei, String wallet, Integer type, Boolean enable, Long pageSize, Long pageNum) {
        ShiroUser user = ShiroKit.getUserNotNull();
        IPage<WalletConfig> page = lambdaQuery()
                .eq(!ShiroKit.isAdmin(), WalletConfig::getCreateBy, user.getAccount())
                .eq(StringUtils.isNotBlank(imei), WalletConfig::getImei, imei)
                .eq(StringUtils.isNotBlank(wallet), WalletConfig::getWallet, wallet)
                .eq(type != null, WalletConfig::getType, type)
                .eq(enable != null, WalletConfig::getEnable, enable)
                .orderByDesc(WalletConfig::getId)
                .page(new Page<>(pageNum, pageSize));
        return LayuiPageFactory.createPageInfo(page);
    }

    @Override
    public LayuiPageInfo dsList(Long pageNum, Long pageSize) {
        IPage<CdInfo> page = cdInfoService.lambdaQuery()
                .orderByDesc(CdInfo::getCreateTime)
                .page(new Page<>(pageNum, pageSize));
        return LayuiPageFactory.createPageInfo(page);
    }

    @Override
    public LayuiPageInfo dfList(Long pageNum, Long pageSize) {
        ShiroUser user = ShiroKit.getUserNotNull();
        IPage<DfInfo> page = dfInfoService.lambdaQuery()
                .eq(!ShiroKit.isAdmin(), DfInfo::getGroupUserName, user.getAccount())
                .orderByDesc(DfInfo::getCreateTime)
                .page(new Page<>(pageNum, pageSize));
        return LayuiPageFactory.createPageInfo(page);
    }
}
